//package cn.nfwx.tv.controller;
//
//import android.os.Handler;
//import android.os.Message;
//import android.support.v4.app.FragmentActivity;
//import android.text.InputFilter;
//import android.text.TextUtils;
//import android.text.method.HideReturnsTransformationMethod;
//import android.text.method.PasswordTransformationMethod;
//import android.util.Log;
//import android.view.View;
//import android.widget.Button;
//import android.widget.CheckBox;
//import android.widget.CompoundButton;
//import android.widget.EditText;
//import android.widget.LinearLayout;
//import android.widget.RelativeLayout;
//import android.widget.Toast;
//
//import com.alibaba.fastjson.JSON;
//import com.alibaba.fastjson.TypeReference;
//import com.google.gson.Gson;
//import com.tencent.mm.opensdk.modelmsg.SendAuth;
//
//import org.greenrobot.eventbus.EventBus;
//import org.json.JSONObject;
//
//import cn.nfwx.tv.R;
//import cn.nfwx.tv.base.BaseFragment;
//import cn.nfwx.tv.base.NFWXApplication;
//import cn.nfwx.tv.callback.ResponseListener;
//import cn.nfwx.tv.callback.UserCallBack;
//import cn.nfwx.tv.conts.Constants;
//import cn.nfwx.tv.events.LoginEvents;
//import cn.nfwx.tv.events.ToForgetPWEvents;
//import cn.nfwx.tv.events.ToMineItemEvent;
//import cn.nfwx.tv.events.ToSmsLoginEvents;
//import cn.nfwx.tv.fragment.me.MineFragment;
//import cn.nfwx.tv.helper.AccountHelper;
//import cn.nfwx.tv.helper.DialogFragmentHelper;
//import cn.nfwx.tv.helper.EditNullFilter;
//import cn.nfwx.tv.helper.NetworkHelper;
//import cn.nfwx.tv.helper.ValueCallback;
//import cn.nfwx.tv.helper.WifiHelper;
//import cn.nfwx.tv.manager.UserManager;
//import cn.nfwx.tv.model.Result;
//import cn.nfwx.tv.model.User;
//import cn.nfwx.tv.model.UserModel;
//import cn.nfwx.tv.util.MD5Util;
//import cn.nfwx.tv.util.NetworkUtil;
//import cn.nfwx.tv.util.SharePrefUtil;
//import cn.nfwx.tv.util.ToastUtil;
//import cn.nfwx.tv.util.ToolUtil;
//import cn.nfwx.tv.vo.ResultErrorVo;
//
///**
// * Created by dell on 2018/1/8.
// */
//
//public class LoginViewController implements View.OnClickListener, NetworkHelper.NetworkStateListener, ResponseListener<JSONObject> {
//    private FragmentActivity activity;
//    private BaseFragment mFragment;
//    private RelativeLayout login_by_sms_lin, login_forget_pw_lin;
//    private EditText et_login_phone, et_login_pwd;
//    private CheckBox login_cb;
//    private LinearLayout login_wechat_lin;
//    private Button login_btn;
//    private View.OnClickListener smsLoginListener, forgetPwListener;
//    private View login_wechat_lin_view;
//    private String requestLoginCommand;
//    private boolean hasPhone = true;
//
//    public LoginViewController(FragmentActivity activity, BaseFragment fragment, View view) {
//        this.activity = activity;
//        mFragment = fragment;
//        initView(view);
//        NetworkHelper.getInstance().addListener(this);
//    }
//
//    public void initView(View view) {
//        login_wechat_lin_view = findView(view, R.id.login_wechat_lin_view);
//        login_by_sms_lin = findView(view, R.id.login_by_sms_lin);
//        login_forget_pw_lin = findView(view, R.id.login_forget_pw_lin);
//        login_by_sms_lin.setOnClickListener(this);
//        login_forget_pw_lin.setOnClickListener(this);
//        et_login_phone = findView(view, R.id.et_login_phone);
//        et_login_pwd = findView(view, R.id.et_login_pwd);
//        login_cb = findView(view, R.id.login_cb);
//        login_btn = findView(view, R.id.login_btn);
//        login_wechat_lin = findView(view, R.id.login_wechat_lin);
//        login_btn.setOnClickListener(this);
//        login_wechat_lin.setOnClickListener(this);
//        et_login_phone.setFilters(new InputFilter[]{new EditNullFilter()});
//        et_login_pwd.setFilters(new InputFilter[]{new EditNullFilter()});
//        login_cb.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
//            @Override
//            public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
//                if (b) {
//                    et_login_pwd.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
//                } else {
//                    et_login_pwd.setTransformationMethod(PasswordTransformationMethod.getInstance());
//                }
//            }
//        });
//
//        ToolUtil.toCheckPhone(activity, et_login_phone, 1, new ValueCallback<Boolean>() {
//            @Override
//            public void onReceiveValue(Boolean value) {
//                hasPhone = value;
//            }
//        });
//        ToolUtil.toSetEditTextLength(activity, et_login_pwd, 15);
//        if (NetworkUtil.getNetworkType(activity) == NetworkUtil.WIFI_NETWORK) {
//            WifiHelper.getInstance()._testIfNeedLogon(new ValueCallback<Boolean>() {
//                @Override
//                public void onReceiveValue(Boolean value) {
//                    if (value) {
//                        shandler.sendEmptyMessage(0);
//                    } else {
//                        shandler.sendEmptyMessage(1);
//                    }
//                }
//            }, "login1");
//        } else {
//            shandler.sendEmptyMessage(0);
//        }
//
//    }
//
//    public <T extends View> T findView(View view, int rid) {
//        return (T) view.findViewById(rid);
//    }
//
//    @Override
//    public void onClick(View v) {
//        switch (v.getId()) {
//            case R.id.login_by_sms_lin:
//                EventBus.getDefault().post(new ToSmsLoginEvents());
//                break;
//            case R.id.login_forget_pw_lin:
//                EventBus.getDefault().post(new ToForgetPWEvents());
//                break;
//            case R.id.login_btn:
//                final String mobileNo = et_login_phone.getText().toString().trim();
//                String userPassword = et_login_pwd.getText().toString().trim();
//                if (TextUtils.isEmpty(mobileNo)) {
//                    Toast.makeText(activity, activity.getResources().getString(R.string.toast_phone), Toast.LENGTH_LONG).show();
//                    return;
//                }
//                if (!ToolUtil.isPhoneLegal(mobileNo)) {
//                    Toast.makeText(activity, activity.getResources().getString(R.string.toast_right_phone), Toast.LENGTH_LONG).show();
//                    return;
//                }
//                if (TextUtils.isEmpty(userPassword)) {
//                    Toast.makeText(activity, activity.getResources().getString(R.string.toast_pw), Toast.LENGTH_LONG).show();
//                    return;
//                }
//                if (!ToolUtil.isPassWordLegal(userPassword)) {
//                    Toast.makeText(activity, activity.getResources().getString(R.string.toast_pw_length), Toast.LENGTH_LONG).show();
//                    return;
//                }
//                int stats = AccountHelper.getInstance().getLiveStats();
//                if (stats == Constants.STATS_P2P) {
//                    DialogFragmentHelper.showLoadingDialog(mFragment.getChildFragmentManager());
//                    UserManager.login(activity, mobileNo, userPassword, new UserCallBack() {
//                        @Override
//                        public void onSuccess(UserModel userModel) {
//                            SharePrefUtil.saveUser(activity, userModel);//缓存用户数据
//                            EventBus.getDefault().post(new LoginEvents(userModel));
//                            activity.runOnUiThread(new Runnable() {
//                                @Override
//                                public void run() {
//                                    DialogFragmentHelper.closeLoadingDialog(mFragment.getChildFragmentManager());
//                                }
//                            });
//                        }
//
//                        @Override
//                        public void onError(String tag, Throwable t) {
//                            super.onError(tag, t);
//                            activity.runOnUiThread(new Runnable() {
//                                @Override
//                                public void run() {
//                                    DialogFragmentHelper.closeLoadingDialog(mFragment.getChildFragmentManager());
//                                    //  Toast.makeText(activity,"登录失败",Toast.LENGTH_LONG).show();
//                                }
//                            });
//                        }
//
//                        @Override
//                        public void onFail(String code, final String response) {
//                            activity.runOnUiThread(new Runnable() {
//                                @Override
//                                public void run() {
//                                    Log.d("onFail", response);
//                                    try {
//                                        Gson gson = new Gson();
//                                        ResultErrorVo resultErrorVo = gson.fromJson(response, ResultErrorVo.class);
//                                        DialogFragmentHelper.closeLoadingDialog(mFragment.getChildFragmentManager());
//                                        Toast.makeText(activity, resultErrorVo.getMsg(), Toast.LENGTH_LONG).show();
//                                    } catch (Exception e) {
//                                        e.printStackTrace();
//                                    }
//                                }
//                            });
//
//                        }
//
//                        @Override
//                        public void onComplete() {
//                            activity.runOnUiThread(new Runnable() {
//                                @Override
//                                public void run() {
//                                    DialogFragmentHelper.closeLoadingDialog(mFragment.getChildFragmentManager());
//                                }
//                            });
//                            super.onComplete();
//                        }
//                    });
//                } else if (stats == Constants.STATS_QPOP) {
//                    if (!hasPhone) {
//                        Toast.makeText(activity, "该号码未注册", Toast.LENGTH_LONG).show();
//                        return;
//                    }
//                    final String md5 = MD5Util.MD5(userPassword);
//                    DialogFragmentHelper.showLoadingDialog(mFragment.getChildFragmentManager());
//                    requestLoginCommand = UserManager.requestIjkLogin(activity, mobileNo, md5, LoginViewController.this);
//                }
//                break;
//            case R.id.login_by_sms:
//                if (smsLoginListener != null) {
//                    smsLoginListener.onClick(v);
//                }
//
//                break;
//            case R.id.login_forget_pw:
//                if (forgetPwListener != null) {
//                    forgetPwListener.onClick(v);
//                }
//
//                break;
//            case R.id.login_wechat_lin:
//                SendAuth.Req req = new SendAuth.Req();
//                req.scope = "snsapi_userinfo";
//                req.state = "nfwx";
//                NFWXApplication.getInstance().getIwxapi().sendReq(req);
//                break;
//        }
//    }
//
//    @Override
//    public void onNetworkChange(int networkType) {
//        if (NetworkUtil.getNetworkType(activity) == NetworkUtil.WIFI_NETWORK) {
//            WifiHelper.getInstance()._testIfNeedLogon(new ValueCallback<Boolean>() {
//                @Override
//                public void onReceiveValue(Boolean value) {
//                    if (value) {
//                        shandler.sendEmptyMessage(0);
//                    } else {
//                        shandler.sendEmptyMessage(1);
//                    }
//                }
//            }, "login2");
//        } else {
//            shandler.sendEmptyMessage(0);
//        }
//    }
//
//    public void onDestroy() {
//        NetworkHelper.getInstance().removeListener(this);
//
//    }
//
//    Handler shandler = new Handler() {
//        @Override
//        public void dispatchMessage(Message msg) {
//            super.dispatchMessage(msg);
//            switch (msg.what) {
//                case 0:
//                    login_wechat_lin.setVisibility(View.VISIBLE);
//                    login_wechat_lin_view.setVisibility(View.VISIBLE);
//                    break;
//                case 1:
//                    login_wechat_lin.setVisibility(View.INVISIBLE);
//                    login_wechat_lin_view.setVisibility(View.INVISIBLE);
//                    break;
//            }
//
//        }
//    };
//
//    @Override
//    public void onResponse(final String requestTag, final JSONObject response) {
//        activity.runOnUiThread(new Runnable() {
//            @Override
//            public void run() {
//                if (requestTag.equals(requestLoginCommand)) {
//                    Result<User> result = JSON.parseObject(response.toString(), new TypeReference<Result<User>>() {
//                    }.getType());
//                    if (Result.RESULT_SUCCESS.equals(result.getMessage())) {
//                        if (result.getData() != null && result.getData().getUid() != 0) {
//                            AccountHelper.getInstance().saveUser(activity, result.getData());
//                            ToastUtil.show(activity, "登录成功", Toast.LENGTH_LONG);
//
//                            EventBus.getDefault().post(new ToMineItemEvent(MineFragment.POSITION_SUBSCRIPT));
//                        }
//                    } else {
//                        ToastUtil.show(activity, "手机号或密码错误");
//                    }
//                }
//            }
//        });
//    }
//
//    @Override
//    public void onError(String requestTag, final Throwable t) {
//        activity.runOnUiThread(new Runnable() {
//            @Override
//            public void run() {
//                ToastUtil.show(activity, "手机号或密码错误", Toast.LENGTH_LONG);
//            }
//        });
//    }
//
//    @Override
//    public void onFinish() {
//        activity.runOnUiThread(new Runnable() {
//            @Override
//            public void run() {
//                if (mFragment.isAdded()) {
//                    DialogFragmentHelper.closeLoadingDialog(mFragment.getChildFragmentManager());
//                }
//            }
//        });
//    }
//}
